package com.lattice.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2022/11/17
 **/
@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfig {
    private static final String SWAGGER_AUTHOR = "mis_wu";
    private static final String SWAGGER_URL = "http://zhangye-wu-aop.cloud/";
    private static final String SWAGGER_EMAIL = "mis_wu1998@163.com";
    private static final String SWAGGER_DESCRIPTION = "@cloud-server-app-swagger-ui- RestFul APIs";
    private static final String SWAGGER_VERSION = "v1.1.0";
    private static final String SWAGGER_GROUP_NAME = "lattice-note-app";
    private static final String SWAGGER_SCAN_PACKAGE = "com.lattice.controller";

    @Bean(value = "swaggerDocket1")
    public Docket defaultApi1() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        //.title("swagger-bootstrap-ui-demo RESTful APIs")
                        .description(SWAGGER_DESCRIPTION)
                        .termsOfServiceUrl(SWAGGER_URL)
                        .contact(new Contact(SWAGGER_AUTHOR,SWAGGER_URL,SWAGGER_EMAIL))
                        .version(SWAGGER_VERSION)
                        .build())
                //配置token 或 请求头 Authorization
                .securitySchemes(securitySchemes())
                //分组名称
                .groupName(SWAGGER_GROUP_NAME)
                .select()
                //只显示添加注解的方法
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_PACKAGE))
                .paths(PathSelectors.any())
                .build();
    }

    //配置请求头的方法
    private List<SecurityScheme> securitySchemes() {
        List<SecurityScheme> apiKeyList = new ArrayList<>();
        apiKeyList.add(new ApiKey("Authorization", "Authorization", In.HEADER.toValue()));
        return apiKeyList;
    }
}
